//
//  ZTXYiChangChuLiAlControllerViewController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/12/7.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXYiChangChuLiAlControllerViewController.h"
#import "ZTXChuLiHeaderCell.h"
#import "ZTXYiChangChuLiImageCell.h"
#import "ZTXMeRedCell.h"
#import "TXPingTaiRedCell.h"
#import "ZTXRedBottomCell.h"
#import "ComplainsRecordsModel.h"
@interface ZTXYiChangChuLiAlControllerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *titLabelArr;
@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, strong) NSString *imgArrStr;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *orderDetailCode;
@property (nonatomic, strong) NSString *parentCode;
@property (nonatomic, strong) NSString *contentStr;
@property (nonatomic,strong) FlexTextView *txtView;

@end

@implementation ZTXYiChangChuLiAlControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BR_RGB_HEX(0xf1f1f1, 1.0f);

    [self baseUI];
}

- (void)baseUI{
    [self.view addSubview:self.tableView];
    [self setTitleName:@"投诉详情"];
    [self setBackLeftButton:@""];
    self.dataArray = [@[] mutableCopy];
    self.contentArr = [@[] mutableCopy];
    self.titLabelArr = @[@"投诉人",@"发生地点",@"发生时间",@"关联单号",@"投诉原因"];
    [self tousuDetailRequest];
   
    

}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, kTopHeight + 10, SCREEN_WIDTH, SCREEN_HEIGHT - kTopHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        //注册单元格
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXChuLiHeaderCell" bundle:nil] forCellReuseIdentifier:@"ZTXChuLiHeaderCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXYiChangChuLiImageCell" bundle:nil] forCellReuseIdentifier:@"ZTXYiChangChuLiImageCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"TXPingTaiRedCell" bundle:nil] forCellReuseIdentifier:@"TXPingTaiRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXMeRedCell" bundle:nil] forCellReuseIdentifier:@"ZTXMeRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXRedBottomCell" bundle:nil] forCellReuseIdentifier:@"ZTXRedBottomCell"];
        
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.contentArr.count;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return self.dataArray.count;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZTXChuLiHeaderCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXChuLiHeaderCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(self.contentArr.count != 0){
            cell.titleLabelx.text = self.titLabelArr[indexPath.row];
            cell.contentLabel.text = self.contentArr[indexPath.row];
        }
        return cell;
    }else if (indexPath.section == 1){
        ZTXYiChangChuLiImageCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXYiChangChuLiImageCell" forIndexPath:indexPath];
        if ([self.imgArrStr isNotBlank]) {
            [cell fuzhiImagArr:self.imgArrStr];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else  if (indexPath.section == 2){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            TXPingTaiRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TXPingTaiRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiDic:dic];
            return cell;
        }else{
            ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiZTXMeRedCell:dic];
            return cell;
        }
    }else{
        ZTXRedBottomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXRedBottomCell" forIndexPath:indexPath];
        [cell.huifuButton addTarget:self action:@selector(huifuButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.txtView = cell.txtView;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
   
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return [ZTXChuLiHeaderCell heightCentent:self.contentArr[indexPath.row]];
    }else if (indexPath.section == 1){
        if ([self.imgArrStr isNotBlank]) {
            return [ZTXYiChangChuLiImageCell cententImagHeight:self.imgArrStr];
        }else{
            return 0;
        }
    }else if (indexPath.section == 2){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            return [TXPingTaiRedCell fuzhiDicHeight:dic];
        }else{
            return [ZTXMeRedCell fuzhiDicHeight:dic];
        }
    }else if (indexPath.section == 3){
        return 200;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)huifuButtonAction:(UIButton *)btn{
    CGPoint point = btn.center;//找到btn中心点记录下来
    point = [self.tableView convertPoint:point fromView:btn.superview];
    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
    ZTXRedBottomCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
    [self huifuTouSu:cell];
}

- (void)huifuTouSu:(ZTXRedBottomCell *)cell{
    if (self.txtView.text.length == 0) {
        [CJAlert showStringDismissWithDelay:@"请填输入回复内容"];
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.code forKey:@"code"];
    [params setValue:self.orderDetailCode forKey:@"orderDetailCode"];
    [params setValue:self.code forKey:@"parentCode"];
    [params setValue:self.txtView.text forKey:@"handleIdea"];

    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/orderDetailAbnormal/handle",CJBaseUrl);
    [[HDNetworking sharedHDNetworking] POST:strUrl parameters:params success:^(id  _Nonnull responseObject) {
        
        cell.txtView.text = @"";
        cell.txtView.placeholder = @"回复：";
        [self tousuDetailRequest];
        
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (void)tousuDetailRequest {
    [self.contentArr removeAllObjects];
    [self.dataArray removeAllObjects];
    WS(ws);
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/orderDetailAbnormal/%@",CJBaseUrl,self.orderCode);
        
    NSDictionary *dicPra = nil;

    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
//        ws.modelComplainsRecord = [[ComplainsRecordsModel alloc]initWithDictionary:responseObject error:nil];
        NSString *shipmentName = [CJRegular getIsStrEmpty:responseObject[@"shipmentName"]] ? @"" : responseObject[@"shipmentName"];
        NSString *driverPhone = [CJRegular getIsStrEmpty:responseObject[@"driverPhone"]] ? @"" : responseObject[@"driverPhone"];
        NSString *driverName = [CJRegular getIsStrEmpty:responseObject[@"driverName"]] ? @"" : responseObject[@"driverName"];
        NSString *address = [CJRegular getIsStrEmpty:responseObject[@"address"]] ? @"" : responseObject[@"address"];
        NSString *occurrenceTime = [CJRegular getIsStrEmpty:responseObject[@"occurrenceTime"]] ? @"" : responseObject[@"occurrenceTime"];
        NSString *orderCode = [CJRegular getIsStrEmpty:responseObject[@"orderCode"]] ? @"" : responseObject[@"orderCode"];
        NSString *abnormalRemark = [CJRegular getIsStrEmpty:responseObject[@"abnormalRemark"]] ? @"" : responseObject[@"abnormalRemark"];
        if (_abnormalFrom == 0) {
            //投诉我的
            [ws.contentArr addObject: CJStringWithFormat(@"%@ (%@)",shipmentName,driverPhone)];
        } else {
            //我投诉的
            [ws.contentArr addObject: CJStringWithFormat(@"%@ (%@)",driverName,driverPhone)];
        }

        [ws.contentArr addObject:address];
        [ws.contentArr addObject:occurrenceTime];
        [ws.contentArr addObject:orderCode];
        [ws.contentArr addObject:abnormalRemark];
        ws.imgArrStr = responseObject[@"abnormalImage"];
        [self.dataArray addObjectsFromArray:responseObject[@"childRenVoList"]];
        self.code = responseObject[@"code"];
        self.orderDetailCode = responseObject[@"orderDetailCode"];
        self.parentCode = responseObject[@"parentCode"];
        [self.tableView reloadData];
        [self.tableView scrollToRow:0 inSection:3 atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}
@end
